﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand.Admin
{
    public class UpdateChecklistRuleHideDocServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateChecklistRuleHideDocRequest, ResponseRecord>
    {
        private UserRecord Dao;
        private string dataUpdate;
        private int productId;
        public UpdateChecklistRuleHideDocServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(UpdateChecklistRuleHideDocRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.DataUpdate))
            {
                throw new ArgumentException("Record must be set", "request");
            }
            else
            {
                dataUpdate = request.DataUpdate;
            }
            if (string.IsNullOrEmpty(request.ProductId) && int.Parse(request.ProductId) == 0)
            {
                throw new ArgumentException("Product must be set", "request");
            }
            else
            {
                productId = int.Parse(request.ProductId);
            }
            if (request.DaoChange == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.DaoChange;
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            try
            {
                bool isUpdate = true;
                var array = JArray.Parse(Request.DataUpdate);
                int ruleId = int.Parse(Request.RuleId);
                ChecklistHideDocWhenAnswer hideDocDocument = new ChecklistHideDocWhenAnswer();
                if (int.Parse(Request.RuleId) != 0 && Request.SubProductId != 0)
                {
                    hideDocDocument = new ChecklistHideDocWhenAnswer
                    {
                        DocHideId = ruleId,
                        ProductId = productId,
                        QuestionId = int.Parse(array[0].ToString().Trim('{', '}')),
                        Answer = array[1].ToString().Trim('{', '}'),
                        DocumentId = array[2].ToString().Trim('{', '}'),
                        SubProductId = Request.SubProductId
                    };
                    if (Providers.ProductHandbooks.CheckRuleHideDocByParamsFromView(hideDocDocument))
                    {
                        isUpdate = Providers.ProductHandbooks.UpdateRuleHideDocChecklist(hideDocDocument);
                    }
                    else
                    {
                        isUpdate = false;
                        response.ErrorMessage = string.Format("Error on params");
                    }
                }
                else
                {
                    hideDocDocument = new ChecklistHideDocWhenAnswer
                    {
                        DocHideId = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds,
                        ProductId = productId,
                        QuestionId = int.Parse(array[0].ToString().Trim('{', '}')),
                        Answer = array[1].ToString().Trim('{', '}'),
                        DocumentId = array[2].ToString().Trim('{', '}'),
                        SubProductId = Request.SubProductId
                    };
                    isUpdate = Providers.ProductHandbooks.AddRuleHideDocChecklist(hideDocDocument);
                    if (isUpdate == false)
                    {
                        response.ErrorMessage = string.Format("Rule is existed");
                    }
                }
                return isUpdate;
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }
        }
    }
}
